package com.zrgj.manager.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.zrgj.common.response.BaseResponse;
import com.zrgj.common.response.StatusCodeEnum;
import com.zrgj.common.utils.NameUtil;
import com.zrgj.domain.*;
import com.zrgj.manager.service.IProductService;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "/admin")
public class ProductController {

    @Reference
    private IProductService productService;

    /**
     * 添加商品
     * @param productAndProductInfo
     * @return
     */
    @RequestMapping(value = "/insertProduct")
    public BaseResponse insertProduct(@RequestBody ProductAndProductInfo productAndProductInfo){
        try {
            Product product = productAndProductInfo.getProduct();
            ProductInfo productInfo = productAndProductInfo.getProductInfo();
            product.setProductShelvesTime(new Date());
            productService.insertPro(product,productInfo);
            System.out.println(product);
            System.out.println(productInfo);

            return new BaseResponse<>(StatusCodeEnum.SUCCESS,productAndProductInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new BaseResponse(StatusCodeEnum.ERROR);
        }
    }



    /**
     * 商品修改回显
     * @return
     */
    @RequestMapping(value = "/showEdit")
    public BaseResponse showEdit(Integer productId){
        ProductPo productPos = productService.selectVo(productId);
        return new BaseResponse(StatusCodeEnum.SUCCESS, productPos);
    }

    /**
     * 修改商品信息
     * @return
     */
    @RequestMapping(value = "/editPro")
    public BaseResponse editPro(@RequestBody ProductPo productPo){
        productService.update(productPo);
        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }

    /**
     * 获取商品列表
     * @return
     */
    @RequestMapping(value = "/getProList")
    public BaseResponse getProList(){
        List<Product> productList = productService.selectPro(new Product());
        return new BaseResponse(StatusCodeEnum.SUCCESS,productList);
    }

    /**
     * 删除商品
     * @param productId
     * @return
     */
    @RequestMapping(value = "/delPro")
    public BaseResponse delPro(Integer productId){
        productService.delete(productId);
        return new BaseResponse(StatusCodeEnum.SUCCESS);
    }
}
